package com.hongfeng.support.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hongfeng.support.annotation.Online;
import com.hongfeng.support.common.Page;
import com.hongfeng.support.controller.common.BaseController;
import com.hongfeng.support.item.ParmItem;
import com.hongfeng.support.item.tag.FieldOperator;
import com.hongfeng.support.item.tag.LogicOperator;
import com.hongfeng.support.model.AutoType;
import com.hongfeng.support.service.AutoTypeService;
import com.hongfeng.support.util.json.JsonUtils;

/**
 * 汽车型号Controller
 * 
 * @author llc
 * 
 */
@Controller
@Online
public class AutoTypeController extends BaseController {

	@Resource
	private AutoTypeService autoTypeService;

	@RequestMapping("list")
	public void list(HttpServletRequest request, HttpServletResponse response,
			String typename, Long autoBrandId, Page<AutoType> page) {
		log.info("查询汽车型号：" + typename + "；汽车品牌ID：" + autoBrandId);
		List<ParmItem> items = new ArrayList<ParmItem>();
		items.add(new ParmItem("name", typename, FieldOperator.LIKE,
				LogicOperator.AND));
		items.add(new ParmItem("autoBrand.id", autoBrandId,
				FieldOperator.EQUAL, LogicOperator.AND));
		autoTypeService.findPageByQuery(page, AutoType.class, items);
		String json = JsonUtils.bean2Json(page);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("listAll")
	public void listAll(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(required = false) Long brandId) {
		List<AutoType> list = null;
		if (brandId == null) {
			list = autoTypeService.findAll();
		} else {
			list = autoTypeService.findByBrandId(brandId);
		}
		String json = JsonUtils.array2Json(list);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("create")
	public void create(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("type") String typeJson) {
		log.info("添加汽车型号:" + (typeJson != null ? typeJson : null));
		if (typeJson == null) {
			this.sendErrorResponse(response, "汽车型号信息不能为空。");
			return;
		}
		AutoType type = (AutoType) JsonUtils.json2Object(typeJson,
				AutoType.class);
		ParmItem item = new ParmItem("name", type.getName(),
				FieldOperator.EQUAL);
		List<AutoType> types = autoTypeService.findAllByQuery(AutoType.class,
				item);
		if (types.size() == 0) {
			if (type.getAutoBrand().getId() == null) {
				type.setAutoBrand(null);
			}
			autoTypeService.create(type);
		} else {
			this.sendErrorResponse(response, "汽车型号已经存在。");
		}
	}

	@RequestMapping("update")
	public void update(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("type") String typeJson) {
		log.info("修改汽车型号:" + (typeJson != null ? typeJson : null));
		if (typeJson == null) {
			this.sendErrorResponse(response, "汽车型号信息不能为空。");
			return;
		}
		AutoType type = (AutoType) JsonUtils.json2Object(typeJson,
				AutoType.class);
		AutoType qType = autoTypeService.read(type.getId());
		List<AutoType> types = null;
		if (!type.getName().equals(qType.getName())) {
			ParmItem item = new ParmItem("name", type.getName(),
					FieldOperator.EQUAL);
			types = autoTypeService.findAllByQuery(AutoType.class, item);
		}
		if (types == null || types.size() == 0) {
			if (type.getAutoBrand().getId() == null) {
				type.setAutoBrand(null);
			}
			type.setCreateTime(qType.getCreateTime());
			type.setCreator(qType.getCreator());
			autoTypeService.update(type);
		} else {
			this.sendErrorResponse(response, "汽车型号已经存在。");
		}
	}

	@RequestMapping("read")
	public void read(HttpServletRequest request, HttpServletResponse response,
			Long id) {
		log.info("读取汽车型号:" + id);
		AutoType type = autoTypeService.read(id);
		String json = JsonUtils.bean2Json(type);
		log.info("汽车型号信息：" + json);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response, String ids) {
		log.info("删除汽车型号:" + (ids != null ? ids.toString() : null));
		if (ids == null || "".equals(ids)) {
			this.sendErrorResponse(response, "请选择要删除的汽车型号。");
			return;
		}
		String[] idArr = ids.split(",");
		for (String id : idArr) {
			autoTypeService.delete(Long.parseLong(id));
		}
	}

}